package com.village.zpark.controller;

import com.github.pagehelper.PageInfo;
import com.village.zpark.common.ResultMapUtil;
import com.village.zpark.pojo.Cadres;
import com.village.zpark.service.CadresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cadres")
public class CadresController {
    @Autowired
    private CadresService cadresService;

    @RequestMapping
    public String Cadres(){
        return "/cadres";
    }

    @RequestMapping(value = "/cadresPage")
    public String CadresPage(){
        return "/cadresPage";
    }

    @RequestMapping(value = "/cadresQueryById")
    public Object cadresQueryById(@RequestParam(name = "id",required = true) Integer id, Model model){
        Cadres cadres = cadresService.findById(id);
        model.addAttribute("obj",cadres);
        return "/cadresPage";
    }

    @RequestMapping(value = "/cadresList")
    @ResponseBody
    public Object CadresList(){
        List<Cadres> cadresLList = cadresService.findAll();
        return ResultMapUtil.getHashMapList(cadresLList);
    }


    @RequestMapping(value = "/addcadres")
    @ResponseBody
    public Object addCadres(Cadres cadres){
        try {
            cadres.setPeriod(new Date());
            int i = cadresService.addCadres(cadres);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/updatecadres")
    @ResponseBody
    public Object updateCadres(Cadres cadres){
        try {
            int i = cadresService.updateCadres(cadres);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }

    @RequestMapping(value = "/deletecadres")
    @ResponseBody
    public Object deleteCadres(Integer id){
        try {
            int i = cadresService.deleteCadresById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e) {
            return ResultMapUtil.getHashMapException(e);
        }
    }


    @RequestMapping(value = "/cadresQueryPage")
    @ResponseBody
    public Map<String, Object> cadresQueryPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String param) {
        PageInfo<Cadres> cadresPageInfo = cadresService.findPage(page, limit, param);
        Map<String,Object> map=new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询数据成功");
        map.put("count", cadresPageInfo.getTotal());
        map.put("data", cadresPageInfo.getList());
        return map;
    }
}
